﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Windows.Controls;

namespace YJTTool.Core.ViewModels
{
    public abstract class DialogBaseViewModel : InheritBase, IDialogAware
    {
        public UserControl CurrentControl { get; private set; }
        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand ConfirmCommand { get; set; }

        /// <summary>
        /// 弹出框基类
        /// </summary>
        /// <param name="title"></param>
        public DialogBaseViewModel()
        {
            CancelCommand = new DelegateCommand(Cancel);
            ConfirmCommand = new DelegateCommand(Confirm);
            // EventAggregator.GetEvent<DialogControlLoadedEvent>().Subscribe(OnViewLoaded);
        }

        /// <summary>
        /// dialog加载完成事件
        /// </summary>
        /// <param name="control"></param>
         public void OnViewLoadedProtected(UserControl control)
        {
            try
            {
                CurrentControl = control;
                OnViewLoaded(control);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 暂存参数
        /// </summary>
        private IDialogParameters _parameters;

        protected internal void SetParameters(Action<IDialogParameters> action)
        {
            if (action != null && _parameters != null)
            {
                action.Invoke(_parameters);
            }
        }

        #region Override
        public string Title => "";
        public bool CanCloseDialog() => true;
        public event Action<IDialogResult> RequestClose;
        public void OnDialogClosed() { }
        public virtual void OnDialogOpened(IDialogParameters parameters)
        {
            _parameters = parameters;
            OnDialogOpenedWithParameters(_parameters);
        }
        protected abstract void OnDialogOpenedWithParameters(IDialogParameters parameters);
        internal protected virtual void OnViewLoaded(UserControl control) { }
        #endregion

        #region 自定义按钮事件
        public virtual void ConfirmWithParameters(Dictionary<string, object> parameters)
        {
            if (parameters != null)
                UpdateParameters(parameters);
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, _parameters));
        }

        public virtual void CancelWithParameters(Dictionary<string, object> parameters)
        {
            if (parameters != null)
                UpdateParameters(parameters);
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, _parameters));
        }

        private void UpdateParameters(Dictionary<string, object> parameters)
        {
            foreach (var parameter in parameters)
            {
                _parameters.Add(parameter.Key, parameter.Value);
            }
        }
        public virtual void Cancel()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, _parameters));
        }
        public virtual void Confirm()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, _parameters));
        }
        #endregion
    }
}
